<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>网上商城</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <style>
        body {
            background-color: #f8f9fa;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
        }
        .navbar {
            margin-bottom: 20px;
        }
        .product-card {
            border: none;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.05);
            transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
            margin-bottom: 20px;
            overflow: hidden; /* 确保图片圆角 */
            height: 100%; /* 确保卡片高度一致 */
        }
        .product-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 16px rgba(0,0,0,0.1);
        }
        .product-card img {
            width: 100%;
            height: 200px; /* 固定图片高度 */
            object-fit: cover; /* 保持图片比例，裁切多余部分 */
            border-top-left-radius: 8px;
            border-top-right-radius: 8px;
        }
        .product-card .card-body {
            padding: 15px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
        }
        .product-card .card-title {
            font-size: 1.15em;
            font-weight: 600;
            margin-bottom: 10px;
            color: #333;
            height: 3em; /* 固定标题高度，防止溢出影响布局 */
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 2; /* 最多显示两行 */
            -webkit-box-orient: vertical;
        }
        .product-card .card-text {
            color: #6c757d;
            font-size: 0.9em;
            height: 4em; /* 固定描述高度 */
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 3; /* 最多显示三行 */
            -webkit-box-orient: vertical;
            margin-bottom: 10px;
        }
        .product-card .price {
            font-size: 1.4em;
            color: #dc3545; /* 醒目的价格颜色 */
            font-weight: bold;
            margin-top: auto; /* 推到底部 */
        }
        .footer {
            background-color: #343a40;
            color: white;
            padding: 20px 0;
            text-align: center;
            margin-top: 50px;
        }
        .no-products-message {
            text-align: center;
            padding: 50px;
            color: #6c757d;
            font-size: 1.2em;
            background-color: #e9ecef;
            border-radius: 8px;
            margin-top: 30px;
        }
    </style>
</head>
<body>
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container-fluid">
            <a class="navbar-brand" href="{:Url('index/index/index')}">网上商城</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <form class="d-flex mx-auto" action="{:Url('index/index/index')}" method="get" role="search" style="max-width: 500px; width: 100%;">
                    <input class="form-control me-2" type="search" placeholder="搜索商品..." aria-label="Search" name="keyword" value="{$Think.request.get.keyword|default=''}">
                    <button class="btn btn-outline-success" type="submit">搜索</button>
                </form>

                <ul class="navbar-nav ms-auto">
                    {if condition="$Think.session.user_id"}
                        <li class="nav-item">
                            <a class="nav-link" href="#">欢迎, {$Think.session.username}</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="{:Url('index/index/cart')}">购物车</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="{:Url('index/index/myOrders')}">我的订单</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="{:Url('index/index/logout')}">退出登录</a>
                        </li>
                    {else/}
                        <li class="nav-item">
                            <a class="nav-link" href="{:Url('index/index/login')}">登录</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="{:Url('index/index/register')}">注册</a>
                        </li>
                    {/if}
                    <li class="nav-item">
                        <a class="nav-link" href="{:Url('admin/index/adminLogin')}">后台管理</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <div class="container mt-4">
        {if condition="!empty($products)"}
            <div class="row row-cols-1 row-cols-md-2 row-cols-lg-4 g-4">
                {foreach name="products" item="product"}
                <div class="col">
                    <div class="card product-card">
                        <a href="{:Url('index/index/productDetail', ['id' => $product['pId']])}">
                            {if condition="!empty($product['pImg'])"}
                                <img src="/tp/public/static/upload/{$product.pImg}" class="card-img-top" alt="{$product.pName}">
                            {else/}
                                <img src="/tp/public/static/img/no_image.png" class="card-img-top" alt="无图片">
                            {/if}
                        </a>
                        <div class="card-body">
                            <h5 class="card-title">
                                <a href="{:Url('index/index/productDetail', ['id' => $product['pId']])}" class="text-decoration-none text-dark">{$product.pName}</a>
                            </h5>
                            <p class="card-text"><?php echo htmlspecialchars_decode($product['pDescr']); ?></p>
                            <div class="d-flex justify-content-between align-items-center">
                                <span class="price">¥ {$product.pPrice}</span>
                                <a href="{:Url('index/index/addToCart', ['id' => $product['pId']])}" class="btn btn-primary btn-sm">
                                    <i class="bi bi-cart-plus-fill me-1"></i> 加入购物车
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
                {/foreach}
            </div>
        {else/}
            <div class="no-products-message">
                <i class="bi bi-box-seam-fill me-2"></i> 没有找到任何商品。
                {if condition="!empty($Think.request.get.keyword)"}
                    <p class="mt-2">您搜索的关键词是 "<strong>{$Think.request.get.keyword}</strong>"，但没有匹配结果。</p>
                    <p class="mt-3"><a href="{:Url('index/index/index')}" class="btn btn-primary">清除搜索并查看所有商品</a></p>
                {else/}
                    <p class="mt-3">当前没有可用的商品。</p>
                {/if}
            </div>
        {/if}
    </div>

    <footer class="footer">
        <div class="container">
            <p>&copy; 2024 网上商城. 保留所有权利。</p>
        </div>
    </footer>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>